using Jupiter.Infrastructure.Permission;
using Jupiter.Infrastructure.Permission.Attributes;
using Jupiter.Infrastructure.Responses;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Wips.AppService.Authorities.Menus;
using Wips.AppService.Authorities.Operations;
using Wips.AppService.Authorities.Users;
using Wips.AppService.Authorities.Users.Requests;
using Wips.AppService.Authorities.Users.Responses;
using Wips.AppService.CommonViewModels;

namespace Wips.MainServer.Controllers.Authorities
{
    /// <summary>
    /// 用户
    /// </summary>
    public class UserController : CustomeBaseController
    {
        private readonly IUserService _service;
        private readonly IMediator _mediator;
        private readonly IMenuService _menuService;
        private readonly IOperationService _operationService;
        private readonly ISecurityContextAccessor _securityContextAccessor;
        private readonly IPermissionCacheService _userCacheService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="service"></param>
        /// <param name="menuService"></param>
        /// <param name="operationService"></param>
        /// <param name="mediator"></param>
        /// <param name="userCacheService"></param>
        /// <param name="securityContextAccessor"></param>
        public UserController(
            IUserService service,
            IMenuService menuService,
            IOperationService operationService,
            IMediator mediator,
            IPermissionCacheService userCacheService,
            ISecurityContextAccessor securityContextAccessor)
        {
            _service = service;
            _mediator = mediator;
            _userCacheService = userCacheService;
            _securityContextAccessor = securityContextAccessor;
            _menuService = menuService;
            _operationService = operationService;
        }

        #region 基础接口

        /// <summary>
        /// 读取分页列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [Permission]
        public async Task<PageResult<GetUserPagesResponse>> GetPagesAsync(
            [FromBody] GetUserPagesRequest request)
        {
            return await _service.GetPagesAsync(request);
        }

        /// <summary>
        /// 根据ID读取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Permission]
        public async Task<GetUserByIdResponse> GetAsync([FromQuery] string id)
        {
            return await _service.GetByIdAsync(id);
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [Permission]
        public async Task<string> PostAsync([FromBody] CreateUserRequest request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        [Permission]
        public async Task<string> PutAsync([FromBody] UpdateUserRequest request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpDelete]
        [Permission]
        public async Task<string> DeleteAsync([FromBody] DeleteUserRequest request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 状态变更
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        [Permission]
        public async Task<string> StatusChangeAsync(
            [FromBody] UserStatusChangeRequest request)
        {
            var result = await _mediator.Send(request);

            // await hubContext.Clients.All.SendAsync("ReceiveMessage", $"{_securityContextAccessor.UserName}",
            //     $"用户[{request.Id}]状态变更");
            // //

            // await hubContext.Clients.User(request.Id).SendAsync("SystemMessage", "sadasdas");

            // await hubContext.Clients.User("611dcaebd74a4680d1a52919").SendAsync("NoticeMessage", "asdasdasda");

            // await hubContext.Clients.User(request.Id).SendSystemMessageAsync("asdasdas");

            return result;
        }

        #endregion

        #region 扩展接口

        /// <summary>
        /// 获取当前登陆人的用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<GetUserResponse> GetUserAsync()
        {
            return await _service.GetUserAsync(_securityContextAccessor.UserId!);
        }

        /// <summary>
        /// 修改个人设置
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<string> UpdateUserSettingAsync([FromBody] UpdateUserSettingRequest request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<string> ChangePasswordAsync([FromBody] ChangePasswordRequest request)
        {
            return await _mediator.Send(request);
        }


        /// <summary>
        /// 读取用户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<SelectViewModel>> GetSelectDataAsync()
        {
            return await _service.GetSelectDataAsync();
        }

        #endregion
    }
}